define(function(require){
  const Handlebars = require('handlebars')
  const $ = require('jquery')
  const earth = require('app/earth')
  const {tick, sleep} = require('app/func')

  let $backbtn = $('#page-details .back-btn')
  let $articledetail = $('#page-details .articledetail')
  let $webglBgRound = $('#webgl-bg-round')
  let $continfo = $('.continfo')
  let $leftnav = $('#page-details .leftnav')

  let inited = false
  let lastObj

  function routeIn(from){
    $('#head-right-search').addClass('center')
    $backbtn.addClass('show')
    $articledetail.addClass('show')
    $leftnav.addClass('show')    
    if(from != 'search'){
      earth.$cont.addClass('moveRight')
      $webglBgRound.addClass('moveRight')
      $('#star-bg').removeClass('hide')
    }
    if(from == 'home'){
      earth.showDetailView()
      earth.earthRotate(true)
    }
    if(!earth.showingDetail){
      earth.showDetailView()
      earth.timeline.hide()
    }
    $('[name=detail-lnav0][value=1]').prop('checked', true)
    if(!inited){
      inited = true
      $backbtn.on('click', function(){history.back()})
      $('[name=detail-lnav0]').on('change', e => {
        if(e.target.value == 1){
          $('.articledetail>.title>p:eq(0)').text(lastObj.title)
          $continfo.text(lastObj.content)
        }
        if(e.target.value == 2){
          $('.articledetail>.title>p:eq(0)').text(lastObj.translatTitle)
          $continfo.text(lastObj.translatContent)
        }
        if(e.target.value == 3){
          $('.articledetail>.title>p:eq(0)').text(lastObj.handTranslatTitle)
          $continfo.text(lastObj.handTranslatContent)
        }
      })
    }
    let id = window.location.href.split('=')[1]
    let type = window.location.href.split('=')[0].split('?')[1]
    if(type == "id"){
        clickart(id)
    }else if(type == "id2"){
      $.post('api/topic/getTopicInfo?id='+id,function(result){
        if(result.success){
          setLnav(result.obj)
          $('.articledetail .source li:eq(1)').hide();
          $('.articledetail .source li:eq(2)').hide();
          $('.articledetail .title p:first').html(result.obj.infoTitle)
          $('.continfo').css('top',(($('.articledetail>.title').height()) + 108)+'px')
          $('.articledetail .title .reltime').html(tiemChange(result.obj.poTime))
          $('.articledetail .source li:eq(0) span').html(result.obj.country)
          $('.articledetail .source li:eq(3)').html("<span>来源：</span>"+result.obj.webSite)   
          $('.continfo').html(result.obj.infoContent)
        }
      })
    }
      
  }

  async function routeOut(to){
    if(to != 'search'){
      earth.$cont.removeClass('moveRight')
      $webglBgRound.removeClass('moveRight')
      $('#star-bg').addClass('hide')
    }
    if(to == 'home'){
      earth.hideDetailView()
      earth.earthRotate(false)
    }
    $backbtn.removeClass('show')
    $leftnav.removeClass('show')
    $articledetail.removeClass('show')
  }

  //时间处理
  window.tiemChange=function(value){
    value=new Date(parseInt(value)); 
    let M  = (value.getMonth()+1 < 10 ? '0'+(value.getMonth()+1) : value.getMonth()+1) + '-';
    let D = (value.getDate() <10 ? '0' + value.getDate():value.getDate())+ ' ';
    let h = (value.getHours() <10 ? '0' + value.getHours():value.getHours())+ ':';
    let m = value.getMinutes() <10 ? '0' + value.getMinutes():value.getMinutes();
    return M + D + h + m
  }
  
  function setLnav(obj){
    lastObj = obj
    if(obj.translatContent){
      $('[name=detail-lnav0][value=2]').parent().show()
    }else{
      $('[name=detail-lnav0][value=2]').parent().hide()
    }
    if(obj.handTranslatContent){
      $('[name=detail-lnav0][value=3]').parent().show()
    }else{
      $('[name=detail-lnav0][value=3]').parent().hide()
    }
  }

  function clickart(id){
    $.post('api/collection/getInfoDetail?id='+id,function(result){
      setLnav(result.obj)
      if(result.success){
        $continfo.html(result.obj.content)
        $('.articledetail .title p:first').html(result.obj.title)
        $('.continfo').css('top',(($('.articledetail>.title').height()) + 108)+'px')
        let time=result.obj.createTime
        $('.articledetail .title .reltime').html(window.tiemChange(time))
        $('.articledetail .source li:first span').html(result.obj.country)
        $('.articledetail .source li:eq(1)').show()
        $('.articledetail .source li:eq(1) span').html(result.obj.webSite)
        let type=result.obj.type
        if(type==1){
          type='法规'
        }else if(type==2){
          type='标准'
        }else if(type==3){
          type='技术'
        }else if(type==4){
          type='威胁'
        }else if(type==5){
          type='新闻事件'
        }
        $('.articledetail .source li:eq(2)').show()
        $('.articledetail .source li:eq(2) span').html(type)
        $('.articledetail .source li:eq(3)').html("<span>情报负责人：</span>"+result.obj.personName) 
      }
    })
  }

  return {routeIn, routeOut}
})